package com.cy.interfacetest.activity;

import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cy.interfacetest.R;

import java.lang.reflect.Method;

public class DeviceOwnerActivity extends AppCompatActivity {

    private DevicePolicyManager devicePolicyManager;
    private ComponentName deviceAdmin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_owner);

        devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        deviceAdmin = new ComponentName(this, DeviceAdminReceiver.class);

        Button checkStatusButton = findViewById(R.id.checkStatusButton);
        checkStatusButton.setOnClickListener(v -> checkDeviceOwnerStatus());


        Button checkButton = findViewById(R.id.clearDeviceOwnerButton);
        checkButton.setOnClickListener(v -> clearDeviceOwner());

        // 添加禁用相机按钮
        Button disableCameraButton = findViewById(R.id.disableCameraButton);
        disableCameraButton.setOnClickListener(v -> toggleCamera());
    }

    private void checkDeviceOwnerStatus() {
        if (devicePolicyManager.isDeviceOwnerApp(getPackageName())) {
            Toast.makeText(this, "此应用是设备所有者", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "此应用不是设备所有者", Toast.LENGTH_SHORT).show();
        }
    }

    private void clearDeviceOwner() {
        devicePolicyManager.clearDeviceOwnerApp(getPackageName());
        Toast.makeText(this, "设备所有者已清除", Toast.LENGTH_SHORT).show();
    }


    // 添加相机控制方法
    private void toggleCamera() {
     /*   if (!devicePolicyManager.isDeviceOwnerApp(getPackageName())) {
            Toast.makeText(this, "需要设备所有者权限", Toast.LENGTH_SHORT).show();
            return;
        }*/
        setDeviceOwnerByReflection();
       /* boolean isCameraDisabled = devicePolicyManager.getCameraDisabled(deviceAdmin);
        devicePolicyManager.setCameraDisabled(deviceAdmin, !isCameraDisabled);
*/
        /*String status = !isCameraDisabled ? "相机已禁用" : "相机已启用";
        Toast.makeText(this, status, Toast.LENGTH_SHORT).show();*/
    }


    private void setDeviceOwnerByReflection() {
        try {
            // 获取 DevicePolicyManager 类
            Class<?> dpmClass = Class.forName("ga.mdm.PolicyManager");

            // 获取 setDeviceOwnerComponent 方法
            Method setDeviceOwnerMethod = dpmClass.getDeclaredMethod("setDeviceOwnerApp", ComponentName.class);
            setDeviceOwnerMethod.setAccessible(true);

            // 调用方法设置设备所有者
            setDeviceOwnerMethod.invoke(devicePolicyManager, deviceAdmin);

            Toast.makeText(this, "设置设备所有者成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "设置失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}